package br.com.classificadosonline.servlet;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import br.com.classificadosonline.*;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.classificadosonline.nucleo.Pessoa;

/**
 * Servlet implementation class cadastroPlano
 */
public class GerenciarPlanoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Sistema sistema = Sistema.getInstance();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GerenciarPlanoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String comando = request.getParameter("comando");
		
		if (comando.equals("listar")) {
		try {
			RequestDispatcher dispatcher = request.getRequestDispatcher("restrito/plano/gerenciarplano.jsp");
			dispatcher.forward(request, response);
			return;
		}
		
		catch (Exception e) {
				RequestDispatcher dispatcher = request.getRequestDispatcher("restrito/plano/gerenciarplano.jsp");
				dispatcher.forward(request, response);
				return;
		}
	}
		
		else if (comando.equals("editar")){
			try{
			request.setAttribute("codigoPlano", request.getParameter("codigoPlano"));
			RequestDispatcher dispatcher = request.getRequestDispatcher("restrito/plano/editarplano.jsp");
			dispatcher.forward(request, response);
			return;
		}
			catch(Exception e) {
				RequestDispatcher dispatcher = request.getRequestDispatcher("restrito/plano/gerenciarplano.jsp");
				dispatcher.forward(request, response);
				return;
			}
		}
			
		else if (comando.equals("excluir")) {
			try{
			int codigo = Integer.parseInt(request.getParameter("codigoPlano"));
			sistema.excluirPlano(codigo);
			RequestDispatcher dispatcher = request.getRequestDispatcher("restrito/plano/gerenciarplano.jsp");
			dispatcher.forward(request, response);
			return;
		}
			catch(Exception e) {
				RequestDispatcher dispatcher = request.getRequestDispatcher("restrito/plano/gerenciarplano.jsp");
				dispatcher.forward(request, response);
				return;
			}
		}
		
		else if (comando.equals("novo")) {
			try{
			RequestDispatcher dispatcher = request.getRequestDispatcher("restrito/plano/novoplano.jsp");
			dispatcher.forward(request, response);
			return;
		} 
			catch(Exception e) {
				RequestDispatcher dispatcher = request.getRequestDispatcher("restrito/plano/gerenciarplano.jsp");
				dispatcher.forward(request, response);
				return;
			} 
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if(request.getParameter("comando").equals("cadastrar")){
		try {
			String nome = request.getParameter("nome");
			int dias = Integer.parseInt(request.getParameter("dias"));	
			float valor = Float.parseFloat(request.getParameter("valor"));
			Sistema sistema = new Sistema();
			sistema.cadastrarPlano(valor, nome, dias);
			RequestDispatcher dispatcher = request.getRequestDispatcher("/gerenciarplano.jsp");
			return;
		}
		
		catch (Exception e) {
			RequestDispatcher dispatcher = request.getRequestDispatcher("/gerenciarplano.jsp");
			dispatcher.forward(request, response);
			return;
		}
	}
		else 
			if(request.getParameter("comando").equals("editar")){
		try {	
			String nome = request.getParameter("nome");
			int dias = Integer.parseInt(request.getParameter("dias"));
			float valor = Float.parseFloat(request.getParameter("valor"));
			int cod = Integer.parseInt(request.getParameter("codigoPlano"));
			Sistema sistema = new Sistema();
			sistema.modificarPlano(cod, valor, nome, dias);
			RequestDispatcher dispatcher = request.getRequestDispatcher("/gerenciarplano.jsp");
			return;
		
		}catch (Exception e){
			RequestDispatcher dispatcher = request.getRequestDispatcher("/gerenciarplano.jsp");
			dispatcher.forward(request, response);
			return;
		}
	}
		else 
			if(request.getParameter("comando").equals("excluir")){
			try {	
				
				int cod = Integer.parseInt(request.getParameter("codigo"));
				Sistema sistema = new Sistema();
				sistema.excluirPlano(cod);
				RequestDispatcher dispatcher = request.getRequestDispatcher("/gerenciarplano.jsp");
				return;
		
			}catch (Exception e){
				RequestDispatcher dispatcher = request.getRequestDispatcher("/gerenciarplano.jsp");
				dispatcher.forward(request, response);
				return;
			}}
		}


		}
	
